<import file="block/common.html" name="url"/>
<div class="layui-fluid">
	<div class="layui-row layui-col-space15">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body ">
					<div class="layui-form">
					<div class="layui-inline layui-show-xs-block">
						<input type="text" name="title" id="title" placeholder="按标题搜索" class="layui-input">
					</div>
					<div class="layui-inline layui-show-xs-block">
						<select name="cid" id="cid" lay-verify="">
							<option value="">所有栏目</option>
							<?php foreach($catelist as $key=>$vo){?>
							  <option value="<?php echo $vo['id'];?>" <?php echo $_GET['cid'] == $vo['id']?'selected=""':''; echo $vo['type']==0 ?'disabled=""':''; ?>> <?php echo $vo['cname'];?></option>
							<?php }?>  
						</select>
					</div>
					<div class="layui-inline layui-show-xs-block">
						<button class="layui-btn" data-type="reload" id="searchBtn"><i class="layui-icon">&#xe615;</i></button>
					</div>
				</div>
				</div>
			</div>
			<script type="text/html" id="toolbarAE">
            <div class="layui-btn-container">
                <?php if(sysrolTF("content_add")){echo "<button class='layui-btn layui-btn-sm data-add-btn'> ".lang('add')."</button>"; }?>
                <!-- <?php if(sysrolTF("content_del")){echo "<button class='layui-btn layui-btn-sm layui-btn-danger data-delete-btn'> 批量删除 </button>";} ?> -->
            </div>
        </script>

		<table class="layui-hide" id="currentTableId" lay-data="{id: 'idReload'}" lay-filter="currentTableFilter"></table>

		<script type="text/html" id="cateUrlTpl"><a href="/list/{{d.cid}}" target="_blank">{{d.catename}}</a></script>
		<script type="text/html" id="contentUrlTpl">
			<a href="/info/{{d.id}}" target="_blank">{{d.title}}</a>
			{{#  if(d.imgbl == 1){ }}<i class='layui-icon layui-icon-picture' style='color:#1E9FFF'></i>{{#  } }}
			{{#  if(d.attribute_a == 1){ }}<span class='layui-badge layui-bg-blue'>头条</span>{{#  } }}
			{{#  if(d.attribute_b == 1){ }}<span class='layui-badge layui-bg-blue'>小头条</span>{{#  } }}
			{{#  if(d.attribute_c == 1){ }}<span class='layui-badge layui-bg-blue'>轮换</span>{{#  } }}
		</script>
		<script type="text/html" id="contentUserTpl">{{d.nickname}}</script>
		<script type="text/html" id="contentGroupTpl">{{d.groupname}}</script>
		<script type="text/html" id="switchTpl">
		  <?php echo sysrolTF("content_switchStatus")?'<input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="审|未审" lay-filter="switchStatus" {{ d.status == 1 ? "checked" : "" }}>':''; ?>
		</script>
		<script type="text/html" id="currentTableBar">
		<?php echo sysrolTF("content_edit")?'<a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">'.lang('edit').'</a>':''; ?>
		<?php echo sysrolTF("content_del")?'<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">'.lang('del').'</a>':''; ?>
        </script>
		</div>
	</div>
</div>
<script>
	layui.use(['table', 'layer', 'jquery', 'form'], function () {
		var table = layui.table,
			$ = layui.jquery,
			layer = layui.layer,
			form = layui.form;

		form.render();

		table.render({
			elem: '#currentTableId',
			//url: '/res/admin/api/table.json',
			url: '<{caUrl("content","index")}>&cid=<{$cid}>&list_json',
			toolbar: '#toolbarAE',
			defaultToolbar: ['filter', 'exports', 'print', {
				title: '提示',
				layEvent: 'LAYTABLE_TIPS',
				icon: 'layui-icon-tips'
			}],
			page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
				//,curr: 5 //设定初始在第 5 页
				, groups: 1 //只显示 1 个连续页码
				, first: false //不显示首页
				, last: false //不显示尾页
			},
			cols: [[
				//{type: "checkbox", width: 50, fixed: "left"},
				{ field: 'id', width: 80, title: 'ID', sort: true },
				{ field: 'catename', width: 120, title: '栏目', templet: '#cateUrlTpl' },
				{ field: 'title', title: '标题', templet: '#contentUrlTpl' },
				{ field: 'nickname', width: 100, title: '发布人', templet: '#contentUserTpl' },
				{ field: 'groupname', width: 100, title: '发布角色', templet: '#contentGroupTpl' },
				{ field: 'time', width: 200, title: '发布时间', sort: true },
				{ field: 'status', width: 100, title: '状态', sort: true, templet: '#switchTpl', unresize: true },
				{ title: '操作', width: 160, templet: '#currentTableBar', fixed: "right", align: "center" }
			]],
			limits: [10, 15, 20, 25, 50, 100],
			limit: 15,
			id: 'tableReload',
			page: true
		});

		// 监听添加操作
		$(".data-add-btn").on("click", function () {
			loadMain('<{caUrl("content","add")}>&view&menuname=内容添加&cid=<?php echo $_GET["cid"];?>');
			return false;
		});
		// 监听删除操作
		$(".data-delete-btn").on("click", function () {
			var checkStatus = table.checkStatus('currentTableId')
				, data = checkStatus.data;
			layer.alert(JSON.stringify(data));
		});

		//监听表格复选框选择
		table.on('checkbox(currentTableFilter)', function (obj) {
			console.log(obj)
		});

		//监听状态操作
		form.on('switch(switchStatus)', function (obj) {
			var data = obj.data;
			//console.log(this.id);
			var checked = 0;
			if (obj.elem.checked) {
				checked = 1;
			}
			$.post('<{caUrl("content","switchStatus")}>', { id: this.id, status: checked }, function (res) {
				if (res.status == 1) {
					layer.tips(res.info + ' 状态为：' + checked, obj.othis);
				} else {
					console.log(res.info);
					layer.tips(res.info, { icon: 2, anim: 6, time: 5000 });
				}
			});
		});

		table.on('tool(currentTableFilter)', function (obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				loadMain('<{caUrl("content","edit")}>&menuname=内容编辑&view&id=' + data.id);
				return false;
			} else if (obj.event === 'delete') {
				layer.confirm('删除确认？', function (index) {
					$.post('<{caUrl("content","del")}>', data, function (res) {
						if (res.status == 1) {
							layer.msg(res.info, { icon: 1, time: 1000 }, function () {
								obj.del();
								layer.close(index);
							});
						} else {
							layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
						}
					});
				});
			}
		});

		//搜索重载表格
		$('#searchBtn').on('click', function() {
			table.reload('tableReload', {
				method : 'get',
				where : {
					title : $('#title').val(),
					cid : $('#cid').val(),
				},
				page: {
					curr: 1
				},
				page : true,
			});
			return false;
		});
	});
</script>